<?php

/* @project : Template
 * @auther  : 青云
 * @date    : 2018/1/10
 * @desc    : 角色模型
 */

namespace app\common\model;

use think\Model;

class Role extends Model
{
    function getAdminIdListByRoleId($role_id)
    {
        $where[] = ['role_id', 'eq', $role_id];
        $list    = (new RoleAdmin())->where($where)->column('admin_id');
        return $list;
    }

    function getRoleList()
    {
        $where[] = ['is_del', 'eq', 0];
        $list    = $this->where($where)->order('sort asc')->select();
        return $list->toArray();
    }

    function getMenuList($role_id)
    {
        $model   = model('role_node');
        $where[] = array('role_node.role_id', 'eq', $role_id);
        $list    = $model->where($where)->get_list();
        return $list;
    }

    function delMenuList($role_id, $menu_list)
    {
        if (empty($menu_list)) {
            return true;
        }

        if (is_array($menu_list)) {
            $menu_list = array_filter($menu_list);
        } else {
            $menu_list = array_filter(explode(",", $menu_list));
        }

        $where[] = array('role_id', 'eq', $role_id);
        $where[] = array('menu_id', 'in', $menu_list);
        $result  = model('RoleMenu')->where($where)->delete();

        if ($result === false) {
            return false;
        } else {
            return true;
        }
    }

    function setMenuList($role_id, $menu_list)
    {
        if (empty($menu_list)) {
            return true;
        }
        if (is_array($menu_list)) {
            $menu_list = array_filter($menu_list);
        } else {
            $menu_list = array_filter(explode(",", $menu_list));
        }

        foreach ($menu_list as $key => $menu_id) {
            $data[$key]['menu_id'] = $menu_id;
            $data[$key]['role_id'] = $role_id;
        }

        $result = model('role_menu')->insertAll($data);

        if ($result === false) {
            return false;
        }
        return true;
    }

    function delRoleByUserList($user_list)
    {
        if (empty($user_list)) {
            return true;
        }

        if (is_array($user_list)) {
            $user_list = array_filter($user_list);
        } else {
            $user_list = array_filter(explode(",", $user_list));
        }

        $where[] = array('admin_id', 'in', $user_list);
        $result  = model('RoleAdmin')->where($where)->delete();

        if ($result === false) {
            return false;
        } else {
            return true;
        }
    }

    function setRoleListByAdminList(array $role_list, array $admin_list)
    {
        if (empty($admin_list)) {
            return true;
        }
        if (empty($admin_list)) {
            return true;
        }

        if (!empty($admin_list)) {
            (new RoleAdmin)->delete($admin_list);
        }

        $admin_list_str = implode(",", $admin_list);
        $role_list_str  = implode(",", $role_list);

        $prefix = config('database.prefix');
        $where  = 'a.id in (' . $role_list_str . ') AND b.id in(' . $admin_list_str . ')';
        $sql    = 'INSERT INTO ' . $prefix . 'role_admin (admin_id,role_id) ';
        $sql    .= ' SELECT a.id, b.id FROM ' . $prefix . 'admin a, ' . $prefix . 'role b WHERE ' . $where;

        $result = $this->execute($sql);

        if ($result === false) {
            return false;
        } else {
            return true;
        }
    }

    /*查询单条数据*/
    public function singleData($where = [], $field='*')
    {
        $result = $this::where($where)->field($field)->find();
        return $result;
    }
}
